package io.file;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileOp {
	@SuppressWarnings("deprecation")
	public static void main(String args[]) throws Exception {
		DataInputStream disKB = new DataInputStream(System.in);

		System.out.println("Enter the number of employees to store:");
		int count = Integer.parseInt(disKB.readLine());

		DataOutputStream dos = new DataOutputStream(new FileOutputStream(
				"data.dat"));

		for (int i = 0; i < count; i++) {
			System.out.println("**** Enter Employee Details *****");
			System.out.print("ID: ");
			dos.writeInt(Integer.parseInt(disKB.readLine()));

			System.out.print("Name: ");
			dos.writeUTF(disKB.readLine());

			System.out.print("Salary: ");
			dos.writeDouble(Double.parseDouble(disKB.readLine()));

			System.out.print("Desig: ");
			dos.writeUTF(disKB.readLine());
		}

		DataInputStream dis = new DataInputStream(new FileInputStream(
				"data.dat"));

		System.out.println("Retreve all data(Yes/No)");
		String all = disKB.readLine();
		if ((all.charAt(0) == 'y') || (all.charAt(0) == 'Y')) {
			System.out.println("**** All Employee Details *****");
			for (int i = 0; i < count; i++) {
				// System.out.println("**** Employee "+(i+1)+" Detail *****");
				System.out.println("ID: " + dis.readInt());
				System.out.println("Name: " + dis.readUTF());
				System.out.println("Salary: " + dis.readDouble());
				System.out.println("Desig: " + dis.readUTF());
			}
		} else {
			System.out.print("Enter the EmpID to display Details:");
			int find = Integer.parseInt(disKB.readLine());
			int found;
			for (int i = 0; i < count; i++) {
				found = dis.readInt();
				if (find == found) {
					System.out.println("**** Employee Detail *****");
					System.out.println("ID: " + found);
					System.out.println("Name: " + dis.readUTF());
					System.out.println("Salary: " + dis.readDouble());
					System.out.println("Desig: " + dis.readUTF());
				} else {
					dis.readUTF();
					dis.readDouble();
					dis.readUTF();
				}
			}
		}
	}
}